package com.coolweather.android;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.coolweather.android.db.City;
import com.coolweather.android.db.Province;

import org.litepal.crud.DataSupport;
import org.litepal.tablemanager.Connector;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ArrayAdapter arrayAdapter;
    private List<Province> provinceList;
    private List<City> cityList;
    private List<String> dataList = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        //初始化
        Connector.getDatabase();
        //生成视图
        ListView list = (ListView) findViewById(R.id.list_province);
        arrayAdapter = new ArrayAdapter<>(MainActivity.this,
                android.R.layout.simple_list_item_1,
                dataList);
        list.setAdapter(arrayAdapter);
        TextView text = (TextView) findViewById(R.id.tx_top);
        Button bt = (Button) findViewById(R.id.bt_huitui);

        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                text.setText("当前选择城市:");
                provinceList = DataSupport.findAll(Province.class);
                dataList.clear();
                for (Province province : provinceList) {
                    dataList.add(province.getProvinceName());
                }
                arrayAdapter.notifyDataSetChanged();
            }
        });
        //生成数据视图
        provinceList = DataSupport.findAll(Province.class);
        Log.d("info", String.valueOf(provinceList.size()));
        if (provinceList.size() > 0) {
            dataList.clear();
            for (Province province : provinceList) {
                dataList.add(province.getProvinceName());
            }
            arrayAdapter.notifyDataSetChanged();
        } else {
            //getDataFunctions();
        }
        provinceList = DataSupport.findAll(Province.class);
        dataList.clear();
        for (Province province : provinceList) {
            dataList.add(province.getProvinceName());
        }
        arrayAdapter.notifyDataSetChanged();
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Province pro1 = provinceList.get(i);
                text.setText("当前选择城市:" + pro1.getProvinceName());
                cityList = DataSupport.where("provinceId=?",
                        String.valueOf(pro1.getProvinceCode())).find(City.class);
                Log.d("info", String.valueOf(cityList.size()));
                if (cityList.size() > 0) {
                    dataList.clear();
                    for (City city : cityList) {
                        dataList.add(city.getCityName());
                    }
                    arrayAdapter.notifyDataSetChanged();
                }
            }
        });
    }



//    private void getDataFunctions() {
//        new Thread(new Runnable() {
//            @Override
//            public <Response> void run() {
//                try {
//                    OkHttpClient client = new OkHttpClient();
//                    Request request = new Request.Builder()
//                            .url("http://guolin.tech/api/china")
//                            .build();
//                    Response response = client.newCall(request).execute();
//                    String responseData = response.body().string();
//                    handleProvinceResponse(responseData);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }).start();
//
//    }

    private void handleProvinceResponse(String responseData) {
    }
}